<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>rabbitmq快速上手 | haijd</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  <meta name="keywords" content="RabbitMQ" />
  
  
  
  
  <meta name="description" content="RabbitMQ快速上手">
<meta name="keywords" content="RabbitMQ">
<meta property="og:type" content="article">
<meta property="og:title" content="RabbitMQ快速上手">
<meta property="og:url" content="http://www.hais2.com/2017/04/11/RabbitMQ_Quick_Start_Guide/index.html">
<meta property="og:site_name" content="haijd">
<meta property="og:description" content="RabbitMQ快速上手">
<meta property="og:locale" content="default">
<meta property="og:image" content="http://ww1.sinaimg.cn/large/c55a7aeely1fej0bq1oryj206404hmwy.jpg">
<meta property="og:updated_time" content="2017-08-02T15:22:33.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="RabbitMQ快速上手">
<meta name="twitter:description" content="RabbitMQ快速上手">
<meta name="twitter:image" content="http://ww1.sinaimg.cn/large/c55a7aeely1fej0bq1oryj206404hmwy.jpg">
  
    <link rel="alternate" href="/atom.xml" title="haijd" type="application/atom+xml">
  
  <link rel="icon" href="/css/images/favicon.ico">
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link href="https://fonts.googleapis.com/css?family=Open+Sans|Montserrat:700" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,300italic,400italic" rel="stylesheet" type="text/css">
  <link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">
  <style type="text/css">
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/9749f0/00000000000000000001008f/27/l?subset_id=2&fvd=n5) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/90cf9f/000000000000000000010091/27/l?subset_id=2&fvd=n7) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/8a5494/000000000000000000013365/27/l?subset_id=2&fvd=n4) format("woff2");font-weight:lighter;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/d337d8/000000000000000000010095/27/l?subset_id=2&fvd=i4) format("woff2");font-weight:400;font-style:italic;}</style>
    
  <link rel="stylesheet" id="athemes-headings-fonts-css" href="//fonts.googleapis.com/css?family=Yanone+Kaffeesatz%3A200%2C300%2C400%2C700&amp;ver=4.6.1" type="text/css" media="all">
  <link rel="stylesheet" href="/css/style.css">

  <script src="/js/jquery-3.1.1.min.js"></script>

  <!-- Bootstrap core CSS -->
  <link rel="stylesheet" href="/css/bootstrap.css" >
  <link rel="stylesheet" href="/css/hiero.css" >
  <link rel="stylesheet" href="/css/glyphs.css" >
  
    <link rel="stylesheet" href="/css/vdonate.css" >
  

</head>

<script>
var themeMenus = {};

  themeMenus["/"] = "Home"; 

  themeMenus["/archives"] = "Archives"; 

  themeMenus["/categories"] = "Categories"; 

  themeMenus["/tags"] = "Tags"; 

  themeMenus["/about"] = "About"; 

</script>


  <body data-spy="scroll" data-target="#toc" data-offset="50">


  <header id="allheader" class="site-header" role="banner">
  <div class="clearfix container">
      <div class="site-branding">

          <h1 class="site-title">
            
              <a href="/" title="haijd" rel="home"> haijd </a>
            
          </h1>

          
            <div class="site-description">Stay Hungry,Stay Foolish</div>
          
            
          <nav id="main-navigation" class="main-navigation" role="navigation">
            <a class="nav-open">Menu</a>
            <a class="nav-close">Close</a>
            <div class="clearfix sf-menu">

              <ul id="main-nav" class="nmenu sf-js-enabled">
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/">Home</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/archives">Archives</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/categories">Categories</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/tags">Tags</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/about">About</a> </li>
                    
              </ul>
            </div>
          </nav>


      </div>
  </div>
</header>




  <div id="container">
    <div id="wrap">
            
      <div id="content" class="outer">
        
          <section id="main" style="float:none;"><article id="post-RabbitMQ_Quick_Start_Guide" style="width: 66%; float:left;" class="article article-type-post" itemscope itemprop="blogPost" >
  <div id="articleInner" class="clearfix post-1016 post type-post status-publish format-standard has-post-thumbnail hentry category-template-2 category-uncategorized tag-codex tag-edge-case tag-featured-image tag-image tag-template">
    
<div class="article-gallery">
  <div class="article-gallery-photos">
    
      <a class="article-gallery-img fancybox" href="http://ww1.sinaimg.cn/large/c55a7aeely1fej0bq1oryj206404hmwy.jpg" rel="gallery_cjie4najm00603swa9u8m1ppz">
        <img src="http://ww1.sinaimg.cn/large/c55a7aeely1fej0bq1oryj206404hmwy.jpg" itemprop="image">
      </a>
    
  </div>
</div>

    
      <header class="article-header">
        
  
    <h1 class="thumb" class="article-title" itemprop="name">
      RabbitMQ快速上手
    </h1>
  

      </header>
    
    <div class="article-meta">
      
	Posted on <a href="/2017/04/11/RabbitMQ_Quick_Start_Guide/" class="article-date">
	  <time datetime="2017-04-11T12:18:59.000Z" itemprop="datePublished">April 11, 2017</time>
	</a>

      
	<span id="busuanzi_container_page_pv">
	  本文总阅读量<span id="busuanzi_value_page_pv"></span>次
	</span>

    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <blockquote>
<p>　　这是一篇简单的快速上手文章，不对RabbitMQ做过多的研究，只对安装使用的要点做一些提示，详细的使用心得，有时间记录。</p>
</blockquote>
<h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><p>　　演示一下windows版本的安装：由于RabbitMQ使用Erlang编写的，需要运行在Erlang运行时环境上，首先需要安装<strong>Erlang</strong>语言，然后安装RabbitMQ的windows版本服务端。<strong><a href="http://www.erlang.org/download.html" target="_blank" rel="noopener">Erlang下载</a></strong>  <strong><a href="http://www.rabbitmq.com/download.html" target="_blank" rel="noopener">RabbitMQ下载</a></strong></p>
<h3 id="以windows-service的方式启动运行"><a href="#以windows-service的方式启动运行" class="headerlink" title="以windows service的方式启动运行"></a>以windows service的方式启动运行</h3><p>　　以windows服务的形式启动RabbitMQ,可以在后台自动运行；我们进入RabbitMQ的安装目录，进入安装目录种的<strong>sbin</strong>目录下，以<strong>管理员权限</strong>打开cmd命令行工具，分别执行下列三条命令：<br><a id="more"></a><br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">rabbitmq-service <span class="keyword">install</span></span><br><span class="line">rabbitmq-service <span class="keyword">enable</span></span><br><span class="line">rabbitmq-service <span class="keyword">start</span></span><br></pre></td></tr></table></figure></p>
<h3 id="允许RabbitMQ与Erlang交互"><a href="#允许RabbitMQ与Erlang交互" class="headerlink" title="允许RabbitMQ与Erlang交互"></a>允许RabbitMQ与Erlang交互</h3><p>　　进入<code>C:\Windows</code>目录下，将<code>.erlang.cookie</code>文件复制到<code>C:\Users\UserName</code>文件夹下(也就是你自己的用户文件夹下面)。</p>
<h3 id="为RabbitMQ创建默认管理员用户并设置密码"><a href="#为RabbitMQ创建默认管理员用户并设置密码" class="headerlink" title="为RabbitMQ创建默认管理员用户并设置密码"></a>为RabbitMQ创建默认管理员用户并设置密码</h3><p>　　执行下面命令创建新管理员用户名密码，授予所有权限，并且设置为管理员用户：<br><figure class="highlight jboss-cli"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">rabbitmqctl  add_user  username  password</span><br><span class="line">rabbitmqctl  <span class="keyword">set</span>_permissions  username  <span class="string">".*"</span>  <span class="string">".*"</span>  <span class="string">".*"</span></span><br><span class="line">rabbitmqctl  <span class="keyword">set</span>_user_tags username administrator</span><br></pre></td></tr></table></figure></p>
<p>　　删除默认的guest用户:<br><figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">rabbitmqctl</span> delete_user guest</span><br></pre></td></tr></table></figure></p>
<p>　　RabbitMQ提供了一个web管理界面，在服务启动后，我们访问<a href="http://localhost:15672/" target="_blank" rel="noopener">http://localhost:15672/</a>输入设置得用户名密码登陆，就可以看到RabbitMQ的所有执行状态了。</p>
<h2 id="Net-Core-中使用RabbitMQ"><a href="#Net-Core-中使用RabbitMQ" class="headerlink" title=".Net(Core)中使用RabbitMQ"></a>.Net(Core)中使用RabbitMQ</h2><h3 id="安装-Net-Core-客户端"><a href="#安装-Net-Core-客户端" class="headerlink" title="安装.Net(Core)客户端"></a>安装.Net(Core)客户端</h3><p>　　RabbitMQ的.Net客户端支持.Net Core，所以使用.Net Core开发也是可以的。打开NuGet安装工具，搜索<strong>RabbitMQ.Client</strong>安装，或者在程序包管理控制台中输入命令安装：<br><figure class="highlight ada"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Install-<span class="keyword">Package</span> <span class="title">RabbitMQ.Client</span></span><br></pre></td></tr></table></figure></p>
<h3 id="Publish"><a href="#Publish" class="headerlink" title="Publish:"></a>Publish:</h3><p>　　首先我们需要编写发布(推送)消息到RabbitMQ的消息队列中的代码：<br><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> 发送消息</span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;typeparam name="T"&gt;</span>发送的数据类型<span class="doctag">&lt;/typeparam&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;param name="t"&gt;</span>要发送的数据<span class="doctag">&lt;/param&gt;</span></span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> Send&lt;T&gt;(T t)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">const</span> <span class="keyword">string</span> exchangeName = <span class="string">"exchangeName"</span>;  <span class="comment">//交换器名称</span></span><br><span class="line">    <span class="keyword">const</span> <span class="keyword">string</span> queueName = <span class="string">"TestQueue"</span>;  <span class="comment">//队列名称,区分队列</span></span><br><span class="line">    <span class="keyword">const</span> <span class="keyword">string</span> hostName = <span class="string">"localhost"</span>;   <span class="comment">//可设置远程地址</span></span><br><span class="line">    <span class="keyword">const</span> <span class="keyword">string</span> userName = <span class="string">"username"</span>;    <span class="comment">//RabbitMQ的用户名</span></span><br><span class="line">    <span class="keyword">const</span> <span class="keyword">string</span> password = <span class="string">"password"</span>;    <span class="comment">//RabbitMQ的密码</span></span><br><span class="line">    <span class="comment">//创建连接</span></span><br><span class="line">    <span class="keyword">var</span> factory = <span class="keyword">new</span> ConnectionFactory() &#123; HostName = hostName, UserName = userName, Password = password &#125;;</span><br><span class="line">    <span class="keyword">using</span> (<span class="keyword">var</span> connection = factory.CreateConnection())</span><br><span class="line">    &#123;</span><br><span class="line">          <span class="keyword">using</span> (<span class="keyword">var</span> channel = connection.CreateModel())</span><br><span class="line">          &#123;</span><br><span class="line">                <span class="comment">//声明消息队列(持久化到磁盘)</span></span><br><span class="line">                channel.ExchangeDeclare(exchangeName, <span class="string">"direct"</span>, durable: <span class="literal">true</span>, autoDelete: <span class="literal">false</span>, arguments: <span class="literal">null</span>);</span><br><span class="line">                channel.QueueDeclare(queueName, durable: <span class="literal">true</span>, exclusive: <span class="literal">false</span>, autoDelete: <span class="literal">false</span>, arguments: <span class="literal">null</span>);</span><br><span class="line">                <span class="comment">//绑定队列</span></span><br><span class="line">                hannel.QueueBind(queueName, exchangeName, routingKey: queueName);</span><br><span class="line">                <span class="comment">//使用protobuf序列化对象为二进制(推荐使用protobuf序列化，使用方法在后面)</span></span><br><span class="line">                <span class="keyword">byte</span>[] result;</span><br><span class="line">                <span class="keyword">using</span> (MemoryStream ms = <span class="keyword">new</span> MemoryStream())</span><br><span class="line">                &#123;</span><br><span class="line">                     Serializer.Serialize(ms, t);</span><br><span class="line">                     result = <span class="keyword">new</span> <span class="keyword">byte</span>[ms.Length];</span><br><span class="line">                     ms.Position = <span class="number">0</span>;</span><br><span class="line">                     ms.Read(result, <span class="number">0</span>, result.Length);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="comment">//设置消息为可持久化的</span></span><br><span class="line">                <span class="keyword">var</span> props = channel.CreateBasicProperties();</span><br><span class="line">                props.Persistent = <span class="literal">true</span>;</span><br><span class="line"></span><br><span class="line">                <span class="comment">//发布</span></span><br><span class="line">                channel.BasicPublish(exchangeName, routingKey: queueName, basicProperties: props, body: result);</span><br><span class="line">           &#125;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>　　以上就是简单的推送消息的代码，其中使用了谷歌的为进制序列化器<strong>protobuf</strong>对类进行序列化，当然推送的数据除了二进制，也可以是字符串。</p>
<h3 id="Consumer"><a href="#Consumer" class="headerlink" title="Consumer:"></a>Consumer:</h3><p>　　接下来我们编写消息队列的接收端代码，接收端代码在windows环境下，一般编写为windows服务启动运行。<br><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">async</span> <span class="keyword">void</span> <span class="title">Consumer</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">      <span class="keyword">const</span> <span class="keyword">string</span> exchangeName = <span class="string">"exchangeName"</span>;  <span class="comment">//交换器名称</span></span><br><span class="line">      <span class="keyword">const</span> <span class="keyword">string</span> queueName = <span class="string">"TestQueue"</span>;  <span class="comment">//队列名称,区分队列</span></span><br><span class="line">      <span class="keyword">const</span> <span class="keyword">string</span> hostName = <span class="string">"localhost"</span>;   <span class="comment">//可设置远程地址</span></span><br><span class="line">      <span class="keyword">const</span> <span class="keyword">string</span> userName = <span class="string">"username"</span>;    <span class="comment">//RabbitMQ的用户名</span></span><br><span class="line">      <span class="keyword">const</span> <span class="keyword">string</span> password = <span class="string">"password"</span>;    <span class="comment">//RabbitMQ的密码</span></span><br><span class="line"></span><br><span class="line">      <span class="keyword">using</span> (<span class="keyword">var</span> connection = factory.CreateConnection())</span><br><span class="line">      <span class="keyword">using</span> (<span class="keyword">var</span> channel = connection.CreateModel())</span><br><span class="line">      &#123;</span><br><span class="line">            channel.ExchangeDeclare(exchangeName, <span class="string">"direct"</span>, durable: <span class="literal">true</span>, autoDelete: <span class="literal">false</span>, arguments: <span class="literal">null</span>);</span><br><span class="line">            channel.QueueDeclare(queueName, durable: <span class="literal">true</span>, exclusive: <span class="literal">false</span>, autoDelete: <span class="literal">false</span>, arguments: <span class="literal">null</span>);</span><br><span class="line">            channel.QueueBind(queueName, exchangeName, routingKey: queueName);</span><br><span class="line"></span><br><span class="line">            <span class="comment">//创建消息接受者</span></span><br><span class="line">            <span class="keyword">var</span> consumer = <span class="keyword">new</span> QueueingBasicConsumer(channel);</span><br><span class="line">            <span class="comment">//把接收者添加到channel中</span></span><br><span class="line">            channel.BasicConsume(queueName, <span class="literal">false</span>, consumer);</span><br><span class="line">            <span class="comment">//轮询接收消息</span></span><br><span class="line">            <span class="keyword">while</span> (<span class="literal">true</span>)</span><br><span class="line">            &#123;</span><br><span class="line">                 <span class="keyword">var</span> res = consumer.Queue.Dequeue();</span><br><span class="line">                 <span class="comment">//protobuf反序列化</span></span><br><span class="line">                 T t;</span><br><span class="line">                 <span class="keyword">using</span> (MemoryStream ms = <span class="keyword">new</span> MemoryStream())</span><br><span class="line">                 &#123;</span><br><span class="line">                     ms.Write(res.Body, <span class="number">0</span>, res.Body.Length);</span><br><span class="line">                     ms.Position = <span class="number">0</span>;</span><br><span class="line">                     t = Serializer.Deserialize&lt;T&gt;(ms);</span><br><span class="line">                 &#125;</span><br><span class="line">                 <span class="comment">//执行操作</span></span><br><span class="line">                 <span class="keyword">var</span> flag = <span class="literal">false</span>;</span><br><span class="line">                 <span class="comment">//可以将要执行的操作写在其他放法中，执行成后返回一个bool值，在最后的代码中判断是否清除该条消息，配合持久化，可以做到消息不丢失</span></span><br><span class="line">                 flag = DoSomething(t);</span><br><span class="line"></span><br><span class="line">                 <span class="comment">//重要！ 控制当前是否释放队列中的数据。BasicConsume方法中的noAck必须为false。</span></span><br><span class="line">                 <span class="comment">//如果忘记这个方法，将会无法释放队列中处理完的数据，造成内存泄露</span></span><br><span class="line">                 <span class="keyword">if</span> (flag)</span><br><span class="line">                 &#123;</span><br><span class="line">                     channel.BasicAck(res.DeliveryTag, <span class="literal">false</span>);</span><br><span class="line">                 &#125;</span><br><span class="line">           &#125;</span><br><span class="line">      &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="扩展"><a href="#扩展" class="headerlink" title="扩展"></a>扩展</h3><p>　　一般情况下，消息的接收端是寄宿在windows服务中的，我们在windows服务中编写的时候，会在<code>OnStart</code>方法中开启消息的监听方法，也就是调用锁编写的<code>Consumer</code>方法，这样写会比较方便一些，如果我们有多个接收端需要同时监听处理消息，那么可以使用<code>Task</code>进行多线程处理：<br><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">protected</span> override <span class="built_in">void</span> OnStart(<span class="built_in">string</span>[] args)</span><br><span class="line">&#123;</span><br><span class="line">      <span class="comment">// <span class="doctag">TODO:</span> 在此处添加代码以执行启动服务所需的关闭操作。</span></span><br><span class="line"></span><br><span class="line">      <span class="comment">//调用消费者处理方法</span></span><br><span class="line">      Consumer();</span><br><span class="line"></span><br><span class="line">      <span class="comment">//假如有多个队列同时启动监听处理，那么就使用下面的多线程方式</span></span><br><span class="line">      <span class="comment">//RabbitMQ应该提供了自身多队列同步处理的方法，知道的同学麻烦指导一下，谢谢。</span></span><br><span class="line">      <span class="keyword">var</span> task = <span class="keyword">new</span> Task(<span class="function"><span class="params">()</span>=&gt;</span>&#123;</span><br><span class="line">          Consumer();</span><br><span class="line">      &#125;);</span><br><span class="line">      <span class="keyword">var</span> task1 = <span class="keyword">new</span> Task(<span class="function"><span class="params">()</span>=&gt;</span>&#123;</span><br><span class="line">          Consumer1();</span><br><span class="line">      &#125;);</span><br><span class="line">      task.Start();</span><br><span class="line">      task1.Start();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="protobuf的使用"><a href="#protobuf的使用" class="headerlink" title="protobuf的使用"></a>protobuf的使用</h2><h3 id="安装-1"><a href="#安装-1" class="headerlink" title="安装"></a>安装</h3><p>　　protobuf同样支持.Net Core，我们可以使用NuGet进行安装，在NuGet管理界面输入<strong>protobuf-net</strong>即可找到该组件，点击安装即可；或者也可以使用程序包管理控制台，输入命令安装：<br><figure class="highlight ada"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Install-<span class="keyword">Package</span> <span class="title">protobuf-net</span></span><br></pre></td></tr></table></figure></p>
<h3 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h3><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">[<span class="meta">ProtoContract</span>]</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">Person</span></span><br><span class="line">&#123;</span><br><span class="line">    [<span class="meta">ProtoMember(1)</span>]</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">string</span> Name &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">    [<span class="meta">ProtoMember(2)</span>]</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> Age &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">    [<span class="meta">ProtoMember(3)</span>]</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">string</span> Sex &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>PS:类上面使用特性标签<code>[ProtoContract]</code>标注，表示这个类可以被protobuf序列化，成员被特性标签<code>[ProtoMember(int tag)]</code>标注，表示是可以被序列化的成员，int参数的作用相当于下标(序列化顺序)不可省略。</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>　　这篇文章简单的介绍了RabbitMQ的使用，是我在使用中查阅官方资料、google搜索以及探索过程中的总结，在这里感谢他们的分享。之后有时间的话，会继续深入研究消息队列，希望能带来有干货的文章，最后贴上一本电子书，大家一起看看学习一下：<a href="https://share.weiyun.com/54592504fbcd60010497ada1c55a2c48" target="_blank" rel="noopener">RabbitMQ实战-高效部署分布式消息队列.pdf</a></p>

      
    </div>
    <footer class="entry-meta entry-footer">
      
	<span class="ico-folder"></span>
    <a class="article-category-link" href="/categories/Message-queue/">Message queue</a>

      
  <span class="ico-tags"></span>
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/RabbitMQ/">RabbitMQ</a></li></ul>

      
        <div id="donation_div"></div>

<script src="/js/vdonate.js"></script>
<script>
var a = new Donate({
  title: '如果觉得我的文章对您有用，请随意打赏。您的支持将鼓励我继续创作!', // 可选参数，打赏标题
  btnText: 'Donate', // 可选参数，打赏按钮文字
  el: document.getElementById('donation_div'),
  wechatImage: '/css/images/wechatPay.png',
  alipayImage: '/css/images/alipay.jpg'
});
</script>
      

      
        
	<section id="comments" class="comment">
	  <div id="disqus_thread">
	  <noscript>Please enable JavaScript to view the <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
	  </div>
	</section>

	<script type="text/javascript">
	var disqus_shortname = 'haijdblog';
	(function(){
	  var dsq = document.createElement('script');
	  dsq.type = 'text/javascript';
	  dsq.async = true;
	  dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
	  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
	}());
	(function(){
	  var dsq = document.createElement('script');
	  dsq.type = 'text/javascript';
	  dsq.async = true;
	  dsq.src = '//' + disqus_shortname + '.disqus.com/count.js';
	  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
	}());
	</script>


      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2017/04/12/Using-the-Visual-Studio-Remote-debugging/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          使用Visual Studio远程调试
        
      </div>
    </a>
  
  
    <a href="/2017/04/10/Java-this&super-keyword/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Java基础：this&amp;super关键字</div>
    </a>
  
</nav>

  
</article>

<!-- Table of Contents -->

  <aside id="sidebar">
    <div id="toc" class="toc-article">
    <strong class="toc-title">Contents</strong>
    
      <ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#安装"><span class="nav-number">1.</span> <span class="nav-text">安装</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#以windows-service的方式启动运行"><span class="nav-number">1.1.</span> <span class="nav-text">以windows service的方式启动运行</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#允许RabbitMQ与Erlang交互"><span class="nav-number">1.2.</span> <span class="nav-text">允许RabbitMQ与Erlang交互</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#为RabbitMQ创建默认管理员用户并设置密码"><span class="nav-number">1.3.</span> <span class="nav-text">为RabbitMQ创建默认管理员用户并设置密码</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Net-Core-中使用RabbitMQ"><span class="nav-number">2.</span> <span class="nav-text">.Net(Core)中使用RabbitMQ</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#安装-Net-Core-客户端"><span class="nav-number">2.1.</span> <span class="nav-text">安装.Net(Core)客户端</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Publish"><span class="nav-number">2.2.</span> <span class="nav-text">Publish:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Consumer"><span class="nav-number">2.3.</span> <span class="nav-text">Consumer:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#扩展"><span class="nav-number">2.4.</span> <span class="nav-text">扩展</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#protobuf的使用"><span class="nav-number">3.</span> <span class="nav-text">protobuf的使用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#安装-1"><span class="nav-number">3.1.</span> <span class="nav-text">安装</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用"><span class="nav-number">3.2.</span> <span class="nav-text">使用</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#总结"><span class="nav-number">4.</span> <span class="nav-text">总结</span></a></li></ol>
    
    </div>
  </aside>
</section>
        
      </div>
      <footer id="footer" class="site-footer">
  

  <div class="clearfix container">
      <div class="site-info">
	      &copy; 2018 haijd All Rights Reserved.
          
            <span id="busuanzi_container_site_uv">
              本站访客数<span id="busuanzi_value_site_uv"></span>人次  
              本站总访问量<span id="busuanzi_value_site_pv"></span>次
            </span>
          
      </div>
      <div class="site-credit">
        Theme by <a href="https://github.com/iTimeTraveler/hexo-theme-hiero" target="_blank">hiero</a>
      </div>
  </div>
</footer>


<!-- min height -->

<script>
    var contentdiv = document.getElementById("content");

    contentdiv.style.minHeight = document.body.offsetHeight - document.getElementById("allheader").offsetHeight - document.getElementById("footer").offsetHeight + "px";
</script>
    </div>
    <!-- <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
    <a href="/categories" class="mobile-nav-link">Categories</a>
  
    <a href="/tags" class="mobile-nav-link">Tags</a>
  
    <a href="/about" class="mobile-nav-link">About</a>
  
</nav> -->
    

<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true
    }
  });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/scripts.js"></script>
<script src="/js/bootstrap.js"></script>
<script src="/js/main.js"></script>








	<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
	</script>






  </div>

  <a id="rocket" href="#top" class=""></a>
  <script type="text/javascript" src="/js/totop.js" async=""></script>
</body>
</html>
